package com.org.api.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.org.api.admin.controller.CmsUserRedisController;
import com.org.api.admin.entity.po.*;
import com.org.api.admin.service.CmsUserMobileBannerService;
import com.org.api.admin.service.CmsUserRedisService;
import com.org.platform.utils.LocalDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @auther dh
 * @date 2022/3/5 11:29
 */
@Slf4j
@Service
public class CmsUserMobileBannerServiceImpl implements CmsUserMobileBannerService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CmsUserRedisService cmsUserRedisService;

    @Override
    public  List<CmsUserMobileBannerVO> getModuleDetailData(CmsUserRedisIndexModule cmsUserRedisIndexModule) {
        String[] areaCodes = {"430050", "420100"};
        String[] userTypes = {"1103", "1102"};

        List<CmsUserMobileBannerDetailVO> onePosList = new ArrayList<>();
        CmsUserMobileBannerDetailVO cmsUserMobileBannerDetailVO = new CmsUserMobileBannerDetailVO();
        cmsUserMobileBannerDetailVO.setBannerDetailId(10000001L);
        cmsUserMobileBannerDetailVO.setPictureUrl("http://www.baidu.com");
        cmsUserMobileBannerDetailVO.setLinkType(1);
        cmsUserMobileBannerDetailVO.setLinkUrl("http://商品详情页");
        cmsUserMobileBannerDetailVO.setIsDefaultBackground(0);
        cmsUserMobileBannerDetailVO.setDefaultBackgroundUrl("http://图片地址");
        cmsUserMobileBannerDetailVO.setOrderSort(1);
        cmsUserMobileBannerDetailVO.setIsLongTerm(CmsUserRedisController.isLongTeam);
        cmsUserMobileBannerDetailVO.setShowStartTime(CmsUserRedisController.startTimeString);
        cmsUserMobileBannerDetailVO.setShowEndTime(CmsUserRedisController.endTimeString);
        cmsUserMobileBannerDetailVO.setBannerPos("1");
        cmsUserMobileBannerDetailVO.setAreaCodes(areaCodes);
        cmsUserMobileBannerDetailVO.setUserTypes(userTypes);
        onePosList.add(cmsUserMobileBannerDetailVO);

        List<CmsUserMobileBannerDetailVO> twoPosList = new ArrayList<>();
        cmsUserMobileBannerDetailVO = new CmsUserMobileBannerDetailVO();
        cmsUserMobileBannerDetailVO.setBannerDetailId(10000002L);
        cmsUserMobileBannerDetailVO.setPictureUrl("http://www.hao123.com");
        cmsUserMobileBannerDetailVO.setLinkType(4);
        cmsUserMobileBannerDetailVO.setLinkUrl("阿莫西林");
        cmsUserMobileBannerDetailVO.setIsDefaultBackground(0);
        cmsUserMobileBannerDetailVO.setDefaultBackgroundUrl("http://图片地址");
        cmsUserMobileBannerDetailVO.setOrderSort(1);
        cmsUserMobileBannerDetailVO.setIsLongTerm(CmsUserRedisController.isLongTeam);
        cmsUserMobileBannerDetailVO.setShowStartTime(CmsUserRedisController.startTimeString);
        cmsUserMobileBannerDetailVO.setShowEndTime(CmsUserRedisController.endTimeString);
        cmsUserMobileBannerDetailVO.setBannerPos("2");
        cmsUserMobileBannerDetailVO.setAreaCodes(areaCodes);
        cmsUserMobileBannerDetailVO.setUserTypes(userTypes);
        twoPosList.add(cmsUserMobileBannerDetailVO);

        List<CmsUserMobileBannerVO> list = new ArrayList<>();
        CmsUserMobileBannerVO cmsUserMobileBannerVO = new CmsUserMobileBannerVO();
        cmsUserMobileBannerVO.setBannerPos("1");
        cmsUserMobileBannerVO.setList(onePosList);
        list.add(cmsUserMobileBannerVO);

        cmsUserMobileBannerVO = new CmsUserMobileBannerVO();
        cmsUserMobileBannerVO.setBannerPos("2");
        cmsUserMobileBannerVO.setList(twoPosList);
        list.add(cmsUserMobileBannerVO);

        cmsUserMobileBannerVO = new CmsUserMobileBannerVO();
        cmsUserMobileBannerVO.setBannerPos("3");
        list.add(cmsUserMobileBannerVO);

        cmsUserMobileBannerVO = new CmsUserMobileBannerVO();
        cmsUserMobileBannerVO.setBannerPos("4");
        list.add(cmsUserMobileBannerVO);

        cmsUserMobileBannerVO = new CmsUserMobileBannerVO();
        cmsUserMobileBannerVO.setBannerPos("5");
        list.add(cmsUserMobileBannerVO);

        return list;
    }

    @Override
    public void invokeModuleDetailData(CmsUserRedisIndexModule cmsUserRedisIndexModule) {
        String moduleDetailKey = getDetailKey(CmsUserModuleEnum.Banner.getModuleType(), cmsUserRedisIndexModule.getModuleConfigId());
        Integer isDelete = cmsUserRedisIndexModule.getIsDelete();
        if (null != isDelete && isDelete.equals(1)) {
            redisTemplate.delete(moduleDetailKey);
            return;
        }
        redisTemplate.delete(moduleDetailKey);
        List<CmsUserMobileBannerVO> cmsUserMobileBannerVOList = (List<CmsUserMobileBannerVO>)cmsUserRedisIndexModule.getModuleDetailData();
        for (CmsUserMobileBannerVO cmsUserMobileBannerVO : cmsUserMobileBannerVOList) {
            // 每个位置都有一个banner列表
            List<CmsUserMobileBannerDetailVO> cmsUserMobileBannerDetailVOList = cmsUserMobileBannerVO.getList();
            if(null == cmsUserMobileBannerDetailVOList||cmsUserMobileBannerDetailVOList.size()<=0){
            	  continue;
            }
            String bannerPos = cmsUserMobileBannerVO.getBannerPos();
            List<CmsUserMobileBannerDetailVO> bannerDetailList = new ArrayList<>();
            for (CmsUserMobileBannerDetailVO cmsUserMobileBannerDetailVO : cmsUserMobileBannerDetailVOList) {
                String banPos = cmsUserMobileBannerDetailVO.getBannerPos();
                if (null == banPos || !banPos.equals(bannerPos)) {
                    continue;
                }
                String moduleDetailUserKey = getDetailUserKey(CmsUserModuleEnum.Banner.getModuleType(), cmsUserMobileBannerDetailVO.getBannerDetailId());
                String[] areaCodes = cmsUserMobileBannerDetailVO.getAreaCodes();
                String[] userTypes = cmsUserMobileBannerDetailVO.getUserTypes();
                String[] userArray = new String[areaCodes.length + userTypes.length];
                int arrayPos = 0;
                for (String areaCode : areaCodes) {
                    userArray[arrayPos] = CmsUserRedisServiceImpl.moduleUserAreaPrefix + areaCode;
                    arrayPos++;
                }
                for (String userType : userTypes) {
                    userArray[arrayPos] = CmsUserRedisServiceImpl.moduleUserTypePrefix + userType;
                    arrayPos++;
                }
                redisTemplate.delete(moduleDetailUserKey);
                stringRedisTemplate.opsForSet().add(moduleDetailUserKey, userArray);
                long expireTime = cmsUserRedisService.getModuleDetailExpireTime(cmsUserMobileBannerDetailVO.getIsLongTerm(), cmsUserMobileBannerDetailVO.getShowEndTime());
                if (expireTime > 0) {
                    redisTemplate.expire(moduleDetailUserKey, expireTime, TimeUnit.MILLISECONDS);
                }
                cmsUserMobileBannerDetailVO.setAreaCodes(null);
                cmsUserMobileBannerDetailVO.setUserTypes(null);
                bannerDetailList.add(cmsUserMobileBannerDetailVO);
            }
            if (bannerDetailList.size() > 0) {
                redisTemplate.opsForHash().put(moduleDetailKey, bannerPos, bannerDetailList);
            }
        }
    }

    @Override
    public CmsUserMobileBannerVO getRedisDataList(CmsUserRedisIndexModule cmsUserRedisIndexModule) {

        String moduleDetailKey = getDetailKey(CmsUserModuleEnum.Banner.getModuleType(), cmsUserRedisIndexModule.getModuleConfigId());
        CmsUserMobileBannerVO cmsUserMobileBannerVO = new CmsUserMobileBannerVO();
        cmsUserMobileBannerVO.setModuleType(CmsUserModuleEnum.Banner.getModuleType());
        cmsUserMobileBannerVO.setOrderSort(cmsUserRedisIndexModule.getOrderSort());
        List<Object> dataList = redisTemplate.opsForHash().values(moduleDetailKey);
        if (null == dataList || dataList.size() <= 0) {
            return cmsUserMobileBannerVO;
        }
        List<CmsUserMobileBannerDetailVO> bannerDetailVOList = new ArrayList<>();
        label:
        for (Object object : dataList) {
            List<CmsUserMobileBannerDetailVO> cmsUserMobileBannerDetailVOList = null;
            try {
                cmsUserMobileBannerDetailVOList = CmsUserRedisServiceImpl.getObjectMapper().readValue(JSONObject.toJSONString(object), new TypeReference<List<CmsUserMobileBannerDetailVO>>() {
                });
                for (CmsUserMobileBannerDetailVO cmsUserMobileBannerDetailVO : cmsUserMobileBannerDetailVOList) {
                    if (null != cmsUserMobileBannerDetailVO && null != cmsUserMobileBannerDetailVO.getIsLongTerm()
                            && cmsUserMobileBannerDetailVO.getIsLongTerm().equals(1)) {
                        continue;
                    }
                    String showStartTime = cmsUserMobileBannerDetailVO.getShowStartTime();
                    String showEndTime = cmsUserMobileBannerDetailVO.getShowEndTime();
                    LocalDateTime startTime = LocalDateUtil.localDateTimeParse(showStartTime, LocalDateUtil.FORMAT_PATTERN1);
                    LocalDateTime endTime = LocalDateUtil.localDateTimeParse(showEndTime, LocalDateUtil.FORMAT_PATTERN1);
                    if (startTime.compareTo(LocalDateTime.now()) > 0) {
                        continue;
                    }
                    if (endTime.compareTo(LocalDateTime.now()) <= 0) {
                        continue;
                    }
                    String moduleDetailUserKey = getDetailUserKey(CmsUserModuleEnum.Banner.getModuleType(), cmsUserMobileBannerDetailVO.getBannerDetailId());

                    boolean areaExists = false;
                    for (String areaCode : CmsUserRedisController.currentAreaCodes) {
                        String areaCodeKey = CmsUserRedisServiceImpl.moduleUserAreaPrefix + areaCode;
                        boolean areaSetExists = stringRedisTemplate.opsForSet().isMember(moduleDetailUserKey, areaCodeKey);
                        if (areaSetExists) {
                            areaExists = true;
                            break;
                        }
                    }
                    boolean userTypeExists = false;
                    for (String userType : CmsUserRedisController.currentUserTypes) {
                        String userTypeKey = CmsUserRedisServiceImpl.moduleUserTypePrefix + userType;
                        boolean userTypeSetExists = stringRedisTemplate.opsForSet().isMember(moduleDetailUserKey, userTypeKey);
                        if (userTypeSetExists) {
                            userTypeExists = true;
                            break;
                        }
                    }
                    if (!areaExists || !userTypeExists) {
                        continue;
                    }
                    bannerDetailVOList.add(cmsUserMobileBannerDetailVO);
                    continue label;
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
        cmsUserMobileBannerVO.setList(bannerDetailVOList);
        return cmsUserMobileBannerVO;
    }
}
